package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.StationMapper;
import com.suyuan.dao.TransitbatchInfoMapper;
import com.suyuan.dao.TransportrouteInforMapper;
import com.suyuan.entity.Station;
import com.suyuan.entity.StationExample;
import com.suyuan.entity.TransportRouteInfor;
import com.suyuan.entity.TransportRouteInforExample;
import com.suyuan.service.TransportRouteInforService;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yang on 2018/2/5.
 */
@Service(value = "transportRouteInforService")
public class TransportRouteInforServiceImpl implements TransportRouteInforService {
    @Resource
    private TransportrouteInforMapper routeMapper;
    @Resource
    private TransitbatchInfoMapper batchMapper;
    @Resource
    private StationMapper stationMapper;

    public List<TransportRouteInfor> list() {
        return routeMapper.selectByExample(new TransportRouteInforExample());
    }

    public int delete(Integer id) {
        //先删除该线路相应物流批次设空
        batchMapper.removeRouteId(id);
        //将该线路的所有站点信息系删除
        StationExample example = new StationExample();
        StationExample.Criteria criteria = example.createCriteria();
        criteria.andRouteidEqualTo(id);
        stationMapper.deleteByExample(example);
        return routeMapper.deleteByPrimaryKey(id);
    }

    public int save(TransportRouteInfor route) {
        return routeMapper.insert(route);
    }

    public PageInfo inquiry(Integer routeId, Integer stationNo, Page page) {
        TransportRouteInforExample example = new TransportRouteInforExample();
        TransportRouteInforExample.Criteria criteria = example.createCriteria();
        if(routeId != null){
            criteria.andRouteidEqualTo(routeId);
        }
        if(stationNo != null){
            criteria.andStationnoEqualTo(stationNo);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<TransportRouteInfor> routes = routeMapper.selectByExample(example);
        //此处把该线路的所有站点名称拼进去
        for(TransportRouteInfor route : routes){
            StationExample stationExample = new StationExample();
            StationExample.Criteria stationExampleCriteria = stationExample.createCriteria();
            stationExampleCriteria.andRouteidEqualTo(route.getRouteId());
            List<Station> stations = stationMapper.selectByExample(stationExample);
           StringBuilder names = new StringBuilder();
            for(Station s : stations){
                names.append("  "+s.getStationname());
            }
            route.setStationName(names.toString());
        }
        PageInfo info = new PageInfo(routes);
        return info;
    }

    public int update(TransportRouteInfor route) {
        return routeMapper.updateByPrimaryKey(route);
    }

    public TransportRouteInfor findById(Integer id) {
        return routeMapper.selectByPrimaryKey(id);
    }

    public void addRouteAndStation(TransportRouteInfor route, String[] stationNames) throws Exception {
        routeMapper.insertSelective(route);
        Integer routeId = route.getRouteId();
        System.out.println("..............................routeId:"+routeId);
        for(int i = 0 ; i< stationNames.length ;i++){
            String name = stationNames[i];
            if(StringUtils.isBlank(name)){
                continue;//跳过空字符串
            }
            Station station = new Station();
            station.setRouteid(routeId);
            station.setStationname(name);
            station.setStationno(i+1);
            stationMapper.insertSelective(station);
        }
    }
}
